package devil.interceptor;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInterceptor extends AbstractInterceptor
{
	private static final long serialVersionUID = 3490468086319440933L;
	public final static String NO_LOGIN = "noLogin";
	private String userKeyName = "user", unloginPageUrl = "index.jsp";
	
	public String intercept(ActionInvocation ai) throws Exception
	{
		if(ai.getInvocationContext().getSession().get(userKeyName) != null)
		{
			return ai.invoke();
		}
		else
		{
			ServletActionContext.getResponse().sendRedirect(unloginPageUrl);
			return NO_LOGIN;
		}
	}
	
	public void setUserKeyName(String userKeyName)
	{
		this.userKeyName = userKeyName;
	}
	
	public void setUnloginPageUrl(String unloginPageUrl)
	{
		this.unloginPageUrl = unloginPageUrl;
	}
}
